/**
 * 所有版权归 广西梧州 陈锦韬 所有
 *
 * @Title: RxHandlerMethod
 * @Package com.rx.core.handler
 * @Description: 函数处理器
 * @author: 陈锦韬
 * @date: 2021\6\11 0011
 * @version V1.0
 * @Copyright: 2021 陈锦韬  All rights reserved.
 */
package com.rx.core.handler;

import com.rx.core.base.RxBaseData;
import com.rx.core.base.RxBaseException;
import com.rx.core.enm.RESULT_CODE;
import com.rx.core.util.EmptyChecker;
import com.rx.core.util.RxUtil;
import com.rx.core.util.SpringUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.SQLIntegrityConstraintViolationException;

/**
 * @author: Administrator
 * @Description: 函数处理器
 * @date: 2021\6\11 0011
 */
@Data
@EqualsAndHashCode(callSuper=false)
@Slf4j
public class RxHandlerMethod extends AbstractHandler implements RxHandlerWraper{
    /**
     * 基本优先级
     */

    private Method method;
    private Object object;

    RxHandlerMethod(Method method,int order){
        init(order,method.getDeclaringClass().getName() + "." +method.getName());
        this.method = method;
    }

    @Override
    public boolean handle(RxBaseData data) {
        try {
            if(EmptyChecker.isEmpty(object)){
                synchronized (this){
                    if(EmptyChecker.isEmpty(object)){
                        String simpleName = RxUtil.StringUtil.firstLower(method.getDeclaringClass().getSimpleName());
                        object = SpringUtil.getBean(simpleName);
                    }
                }
            }
            return (boolean)method.invoke(object,data);
        } catch (Throwable e) {
            RxUtil.CheckUtil.dealThrowable(e);
        }
        return false;
    }

    @Override
    public boolean equals(Object object){
        RxHandlerMethod rxHandlerMethod = (RxHandlerMethod)object;
        if (this.method.equals(rxHandlerMethod.getMethod())){
            return true;
        }
        return false;
    }
    @Override
    public int hashCode() {
        return this.method.hashCode();
    }
}
